<?php
/**
* PHP version 5
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package Textbook
* @subpackage General
* @author Aljaz Srebrnic <a2piratesoft@gmail.com>;
* @author E. Pavletič
* @copyright 2008 a2PirateSoftware
* @license GPL License v3 <http://www.gnu.org/copyleft/gpl.html>
*/

require_once('class.CommonAction.php');
require_once(ZNF::MODULES_DIR.'/User/Business/UserDAO.php');
require_once(ZNF::MODULES_DIR.'/User/Business/RoleDAO.php');

class User_Action_UserManager extends CommonAction{

    private $userDAO;
    private $roleDAO;

    public function __construct(){
		parent::__construct();
        $this->userDAO = new User_Business_UserDAO();
        $this->roleDAO = new User_Business_RoleDAO();
    }

    /**
     * Metoda služi za pregled uporabnikov. Dobi Constants::limit uporabnikov in jih prikaze s pagerjem.
     **/

    public function userList($form, ZNF_Action_ActionMapping $mapping)
    {
	$id = 1;

	if(isset($_GET['fp'])){
	    $_REQUEST['first'] = true;
	}

	if(isset($_GET['pageID'])) {
	    $id = intval($_GET['pageID']);
	}

	$offset = Constants::$limit * ($id-1); //Stran 1 je v resnici stran 0 :-D

	try {
	    $total = $this->userDAO->countUsers();
	    $users = $this->userDAO->getAll($offset, Constants::$limit);
	    $this->logger->debug('Just got some users form database');
	} catch (User_Business_UserDAOException $e){
	    $_REQUEST['errors'] = self::handleException($e, 'User');
	    return $mapping->findForward('failure');
	}
	$_REQUEST['pageID'] = $id;
	$_REQUEST['users'] = $users;
	$_REQUEST['pager'] = $this->getPagerLinks($total, 'user', 'list');
	return $mapping->findForward('success');
    }

	/**
	*Metoda zbrise uporabnike.
	*
	**/

    public function delete(ZNF_Action_ActionForm $form, ZNF_Action_ActionMapping $mapping)
	{
	$userIds = $form->getIds();

	try{
		$this->userDAO->deleteUsers($userIds);
	} catch(User_Business_UserDAOException $ude){
	    $_REQUEST['errors'] = self::handleException($ude, 'User');
	    return $mapping->findForward('failure');
	}
	//$_REQUEST['ids'] = $userIds;
	$_REQUEST['value'] = true;
	return $mapping->findForward('success');
    }


    /**
     *Metoda isce uporabnike po danem nizu (username in priimek)
     *
     **/
    public function search($form, ZNF_Action_ActionMapping $mapping) {

    	if(!isset($_GET['text'])){
    		$translation = new ZNF_Presentation_Translation('User', $_SESSION['znf']['lang']);
		    $errors = new ZNF_Action_ActionErrors();
		    $errors->addError(1, $translation->get('errorUserSearch'));
		    $_REQUEST['errors'] = $errors;
		    return $mapping->findForward('failure');
    	}

    	$text = htmlspecialchars(stripslashes($_GET['text']));
		$this->logger->debug('Got a query: ' . $text);
    	try{
    		$res = $this->userDAO->search($text);
    	} catch (User_Business_UserDAOException $e){
    		$_REQUEST['errors'] = self::handleException($e, 'User');
    		return $mapping->findForward('failure');
    	}
		$this->logger->debug('Got results: ' . print_r($res, true));
    	$_REQUEST['users'] = $res;
    	return $mapping->findForward('success');

    }

    /**
     *Metoda vrne vse podatke o uporabniku.
    **/

    public function get(ZNF_Action_ActionForm $form, ZNF_Action_ActionMapping $mapping) {

	try{
	    $user = $this->userDAO->getById($form->getId());
	} catch (User_Business_UserDAOException $e){
	    $_REQUEST['errors'] = self::handleException($e, 'User');
	return $mapping->findForward('failure');
	}
	//echo("GET:" . $_GET['pageID']);
	//echo("Form: " . $form->getPage());
	//echo("Intval get: " . intval($_GET['pageID']));
	//die();
	$_REQUEST['pageID'] = $form->getPage();
	$_REQUEST['user'] = $user;
	$_REQUEST['key'] = self::sendToken(true);
	return $mapping->findForward('success');
    }

    /**
     *metoda posodobi informacije o uporabniku (metoda je misljena za administratorje)
    **/

    public function update(ZNF_Action_ActionForm $form, ZNF_Action_ActionMapping $mapping){
        try{
            $this->userDAO->update($form->getId(), $form->getName(), $form->getSurname(),
                                   $form->getPhone(), $form->getCell(), $form->getEmail(), $form->getIcq(),
                                   $form->getMsn(), $form->getSkype(), $form->getComment(), $form->getActive(), $form->getPassword());
        }catch(User_Business_UserDAOException $e){
            $_REQUEST['errors'] = self::handleException($e, 'User');
            return $mapping->findForward('failure');
        }
	$_GET['fp'] = 't';
        return $mapping->findForward('success');
    }

}

?>